﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Thrift.IL
{
    /// <summary>
    /// 序列化与反序列化到文件
    /// </summary>
    public class SerializationHelper
    {

        /// <summary>
        /// 二进制序列化到磁盘
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="obj"></param>
        public static void Serializable(string fileName, object obj)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                new XmlSerializer(obj.GetType()).Serialize(fs, obj);
                //formatter.Serialize(fs, obj);
            }
        }
        /// <summary>
        /// 二进制反序列化从磁盘到内存对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static object Deserialize(string fileName)
        {
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    //  return formatter.Deserialize(fs);
                    return new XmlSerializer(typeof(object)).Deserialize(fs);
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #region XML
        /// <summary>
        /// XML序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToXml(object obj)
        {
            string s = "";
            using (MemoryStream ms = new MemoryStream())
            {

                try
                {
                    XmlSerializer serializer = new XmlSerializer(obj.GetType());
                    ms.Seek(0, SeekOrigin.Begin);
                    serializer.Serialize(ms, obj);
                    s = Encoding.ASCII.GetString(ms.ToArray());
                }
                catch (SerializationException e)
                {
                    Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                    throw;
                }
            }
            return s;
        }
        /// <summary>
        /// XML返序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static object DeserializeFromXml(Type type, string s)
        {
            object o = new object();

            try
            {
                XmlSerializer serializer = new XmlSerializer(type);
                o = serializer.Deserialize(new StringReader(s));
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }

            return o;
        }
        #endregion

        /// <summary>
        /// 添加日志文件
        /// </summary>
        /// <param name="text">要添加的内容</param>
        /// <param name="fileName">文件的相对路径需带后缀名</param>
        /// <param name="isService">1：服务器自动执行时调用，0：服务器不自动执行时调用</param>
        /// <returns></returns>
        public static bool AddLogFiles(string filePath, string msg)
        {
            try
            {
                string dir = filePath.Substring(0, filePath.LastIndexOf("\\"));
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                using (System.IO.StreamWriter srFile = new System.IO.StreamWriter(filePath, true))
                {
                    srFile.WriteLine(msg);
                    srFile.Close();
                    srFile.Dispose();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;

            }


        }

    }
}
